clear;  clc; close all;

 projectdir   = '/Volumes/NO NAME';
 projectdirDB = '/Users/tamoni/Dropbox';

sample=[ 1961 1; 2019  12];

data    = xlsread('./VARdataBBupdatedMonthly.xlsx','MainData');
dataInd = xlsread('./VARdataBBupdatedMonthly.xlsx','IndicatorsContinuous');
startsample=find(data(:,1)==sample(1,1)*100+sample(1,2));
endsample  =find(data(:,1)==sample(2,1)*100+sample(2,2));

data = [data [NaN(12,1); data(13:end,4)./data(1:end-12,4)]];

data    = data(   startsample:endsample,2:end);
dataInd = dataInd(startsample:endsample,2:end);
clear startsample endsample 

 ADS          = dataInd(:,1);
 CFNAI        = dataInd(:,2); % short history, starting only in 1967
 pmi          = dataInd(:,3);
unemp         = dataInd(:,4); %it misses the recession 1969/12, 1973/11, 1990/07, 2001/03, and the first half of 2007/12 
unemphp       = dataInd(:,5);
 GDPpo        = dataInd(:,6); % log difference of GDP from potential
 IPGmaA       = dataInd(:,7); % moving average of annual  growth in industrial production 
 IPGmaM       = dataInd(:,8); % moving average of monthly growth in industrial production 
%corr(IPGmaA(2:end,1),IPGmaM(1:end-1,1))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%DATA TRANSFORMATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data(:,[3:11 12]) = log(data(:,[3:11 12]))*100;
              LevelVsFirstDiff='IPGGrowth';
%'Industrial production'              
if     strcmp(LevelVsFirstDiff,'IPGLevel')
    posIP = 3;
elseif strcmp(LevelVsFirstDiff,'IPGGrowth')
    posIP = 12;
end
% IPGmaA_bis=chgfreq(data(:,posIP),6,1,0)/6;

%% 
[Years,M] = meshgrid(1961:2019, 1:12);
calM = datenum([Years(:), M(:), 28*ones(numel(Years),1)]); % The Serial Dates

ticks = datenum([(1962:5:2019)', 12*ones(12,1), 31*ones(12,1) ]);
%lim_x =[datenum(  1961,12,1), datenum(2019,12,31)];
 lim_x =[datenum(  1966,12,1), datenum(2019,12,31)]; % For CFNAI

COLOR = linspecer(5);
LineWidth =  4.0;
%-------------------------------------------------------------------------%
figure(41)
%h_1 = line(calM(1:end), ADS, 'color', COLOR(1,:), 'LineStyle','-', 'linewidth',LineWidth); hold on
%h_2 = line(calM(1:end),zeros(size(calM(1:end),1),1),'color','k','linewidth',LineWidth);
%-------------------------------------------------------------------------%
% h_1 = line(calM(1:end), CFNAI, 'color', COLOR(1,:), 'LineStyle','-', 'linewidth',LineWidth); hold on
% h_2 = line(calM(1:end),zeros(size(calM(1:end),1),1),'color','k','linewidth',LineWidth);
%-------------------------------------------------------------------------%
% %h_1 = line(calM(1:end), IPGmaA, 'color', COLOR(1,:), 'LineStyle','-', 'linewidth',LineWidth); hold on
%  h_1 = line(calM(1:end), IPGmaM, 'color', COLOR(1,:), 'LineStyle','-', 'linewidth',LineWidth); hold on
%  h_2 = line(calM(1:end),zeros(size(calM(1:end),1),1),'color','k','linewidth',LineWidth);
%-------------------------------------------------------------------------%
%h_1 = line(calM(1:end-1), pmi(2:end), 'color', COLOR(1,:), 'LineStyle','-', 'linewidth',LineWidth); hold on
%h_2 = line(calM(1:end),50*ones(size(calM(1:end),1),1),'color','k','linewidth',LineWidth);
%-------------------------------------------------------------------------%
h_1 = line(calM(1:end), unemp(1:end), 'color', COLOR(1,:), 'LineStyle','-', 'linewidth',LineWidth); hold on
h_2 = line(calM(1:end),6.5*ones(size(calM(1:end),1),1),'color','k','linewidth',LineWidth);

% title('cumulative difference e\_(1)^2 - e\_(3)^2')

uistack(h_1, 'top')

set(gca, 'XLim' , lim_x)
set(gca, 'XTick', ticks)
%set(gca, 'YTick', [-0.01 0 0.01 0.03 0.05 0.07 0.09 0.11])
datetick('keepticks','keeplimits')
box on
recessionplot

set(gca,'FontSize',36)

% legend([h_1,h_2,h_3],{'Convergenge gap (Potential GDP from one-sided HP filter)','Convergenge gap (Potential GDP from Laubach and Williams, 2003)','Convergenge gap (Natural rate from Laubach and Williams, 2003)'})

% ylim([-3.1 2]); % For CFNAI

STOP
%-------------------------------------------------------------------------%
fprintf('Match between      pmi <50   and ADS is %3.2f (%%)\n',sum((pmi(1:end-1)<50)&(ADS(1:end-1)<0))/sum(ADS(1:end-1)<0))
fprintf('Match between      pmi <MED  and ADS is %3.2f (%%)\n',sum((pmi(1:end-1)<median(pmi))&(ADS(1:end-1)<0))/sum(ADS(1:end-1)<0))
fprintf('Match between      pmi <44.5 and ADS is %3.2f (%%)\n',sum((pmi(1:end-1)<44.5)&(ADS(1:end-1)<0))/sum(ADS(1:end-1)<0))
fprintf('Match between LEAD(pmi)<44.5 and ADS is %3.2f (%%)\n',sum((pmi(2:end)  <44.5)&(ADS(1:end-1)<0))/sum(ADS(1:end-1)<0))

fprintf('Match between      pmi <44.5 and IPGmaA is %3.2f (%%)\n',sum((pmi(1:end-1)<44.5)&(IPGmaA(1:end-1)<0))/sum(IPGmaA(1:end-1)<0))
fprintf('Match between      pmi <44.5 and IPGmaM is %3.2f (%%)\n',sum((pmi(1:end-1)<44.5)&(IPGmaM(1:end-1)<0))/sum(IPGmaM(1:end-1)<0))
%-------------------------------------------------------------------------%
  ADS_ss  = ADS(  75:end);
  pmi_ss  = pmi(  75:end);
CFNAI_ss= CFNAI(75:end);
fprintf('Match between CFNAI          and ADS is %3.2f (%%)\n',sum((CFNAI_ss(1:end-1)<0)&(ADS_ss(1:end-1)<0))/sum(CFNAI_ss(1:end-1)<0))
% 85% of the times CFNAI is negative also ADS are negative

% fprintf('Match between      pmi <50   and ADS is %3.2f (%%)\n',sum((pmi_ss(1:end-1)<50)&(ADS_ss(1:end-1)<0))/sum(ADS_ss(1:end-1)<0))
% fprintf('Match between      pmi <MED  and ADS is %3.2f (%%)\n',sum((pmi_ss(1:end-1)<median(pmi_ss))&(ADS_ss(1:end-1)<0))/sum(ADS_ss(1:end-1)<0))
% fprintf('Match between      pmi <44.5 and ADS is %3.2f (%%)\n',sum((pmi_ss(1:end-1)<44.5)&(ADS_ss(1:end-1)<0))/sum(ADS_ss(1:end-1)<0))
% fprintf('Match between LEAD(pmi)<44.5 and ADS is %3.2f (%%)\n',sum((pmi_ss(2:end)  <44.5)&(ADS_ss(1:end-1)<0))/sum(ADS_ss(1:end-1)<0))
fprintf('Match between      pmi <44.5 and CFNAI is %3.2f (%%)\n',sum((pmi_ss(1:end-1)<44.5)&(CFNAI_ss(1:end-1)<0))                        /sum(CFNAI_ss(1:end-1)<0))
CFNAI_diff = [0; CFNAI(2:end) - CFNAI(1:end-1)];
fprintf('Match between      pmi <44.5 and CFNAI is %3.2f (%%)\n',sum((pmi_ss(1:end-1)<44.5)&(CFNAI_ss(1:end-1)<0)&(CFNAI_diff(1:end-1)<0))/sum((CFNAI_ss(1:end-1)<0)&(CFNAI_diff(1:end-1)<0)));